<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01_forceUpdate</title>
</head>

<body>
    <div id="app"></div>

    <script src="../../00_课程资料/js/react.development.js"></script>
    <script src="../../00_课程资料/js/react-dom.development.js"></script>
    <script src="../../00_课程资料/js/babel.min.js"></script>

    <script type="text/babel">
        class Count extends React.Component{
            state = {
                count:0
            }

            add = ()=>{
                let {count} = this.state
                this.setState({
                    count:count++
                })
            }

            deathComponent = ()=>{
                // 卸载组件
                ReactDOM.unmountComponentAtNode(document.getElementById('app'))
            }

            forceComponent = ()=>{
                // 直接让状态中的count等于10
                this.state.count = 10
                // 调用React原型链上的方法，不更改状态的前提下强制更新组件
                this.forceUpdate()
            }

            // 组件将要挂载
            componentWillMount(){
                console.log('组件将要挂载 - componentWillMount');
            }

            // 组件正在挂载
            render(){
                console.log('组件正在挂载 - render');
                return (
                    <div>
                        <h2>数据和：{this.state.count}</h2> 
                        <button onClick={this.add}>点我加1</button>   
                        <button onClick={this.deathComponent}>卸载组件</button>
                        <button onClick={this.forceComponent}>强制更新</button>
                    </div>
                )
            }

            // 组件挂载完成
            componentDidMount(){
                console.log('组件挂载完成 - componentDidMount');
            }

            // 组件更新前
            componentWillUpdate(){
                console.log('组件更新前 - componentWillUpdate');
            }

            // 组件更新完成
            componentDidUpdate(){
                console.log('组件更新完成 - componentDidUpdate');
            }

            // 组件将要卸载
            componentWillUnmount(){
                console.log('组件将要卸载 - componentWillUnmount');
            }
        }

        ReactDOM.render(<Count/>,document.getElementById('app'))
    </script>
</body>

</html>